package Servidor;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;

public class ServidorColor {
	
	private ServerSocket server;
	private final int PORT = 7910;
	
	public ServidorColor() {
		
		iniciarServer();
		aceptarCliente();
		cerrarServer();
	}

	void iniciarServer() {
    	
		try {
    	
			server = new ServerSocket(PORT);
    		System.out.print("HostAddress: ");
    		System.out.print(InetAddress.getLocalHost().getHostAddress());
    		System.out.print("\tPort: ");
     		System.out.println(server.getLocalPort());
     		
       } catch (IOException e) {
    	
    	   	System.out.println(e.toString());
    		System.exit(1);
    	}     	
	}
	
	void aceptarCliente() {
    	
		try {
    	
			while(true){
    			new Sesion(server.accept()).start();
    		}
    	} catch (SocketTimeoutException e) {
    		
    		System.out.println(e.toString());
     	
    	} catch (IOException e) {
     		
     		System.out.println("accept IOException--> " + e.toString());
    	}     	
	}	
	
	void cerrarServer() {
    	try {
    		
      		server.close();
    		System.out.print("ServerSocket cerrado:\t");
    		System.out.println(server.isClosed()); 
    	} catch (IOException e) {
    		
    		System.out.println(e.getMessage());
    		System.exit(1);
    	}     	
	}
	
	public static void main(String[] args) {
		
		new ServidorColor();
		System.exit(0);
	}

}